package com.xpjhs.user.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;





import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xpjhs.user.dao.UserDao;
import com.xpjhs.user.model.User;

@Service
@Transactional(readOnly = true)
public class UserService {
	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	private UserDao userDao;

	/**
	 * 查找所有的城市
	 * 
	 * @return
	 */
	public List list() {

		return userDao.allCity();
	}

	/**
	 * 根据城市查找人数
	 * 
	 * @param city
	 * @return
	 */
	public List cityCount(String city) {
		return userDao.findByCity(city);
	}

	/**
	 * 添加
	 * 
	 * @param user
	 */
	public void save(User user) {
		
		userDao.save(user);
	}
	 @Cacheable(value="myCache", key="'usernameIsExist'+#username")
	public boolean usernameIsExist(String username) {
		User user = userDao.findUserByUsername(username);
		if (user == null) {
			return true;
		}
		return false;
	}

	/**
	 * 用户登录
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public User user(String username, String password) {
		return userDao.findUserBy(username, password);
	}

	/**
	 * 导入用户
	 * 
	 * @param user
	 * @throws FileNotFoundException
	 * @throws IOException
	 */

	public void importUser(User user) throws FileNotFoundException, IOException {

		// 初始化整个Excel
		HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File(
				"d:/2.xls")));
		// 循环workbook内的所有sheet
		for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
			HSSFSheet sheet = workbook.getSheetAt(sheetIndex);
			System.err.println("sheet序号:" + sheetIndex + "\nsheet名称："
					+ sheet.getSheetName());
			// 循环该sheet表中的每一行
			for (int rowIndex = 0; rowIndex < sheet.getLastRowNum(); rowIndex++) {
				HSSFRow row = sheet.getRow(rowIndex);
				if (row == null) {
					continue;
				}
				String qq = "";
				String realname = "";
				String qqName = "";
				String sex = "";
				String qqage = "";
				HSSFCell cell1 = row.getCell(1);
				if (cell1 != null) {

					cell1.setCellType(cell1.CELL_TYPE_STRING);
					qqName = cell1.getRichStringCellValue().toString();
				}
				HSSFCell cell2 = row.getCell(2);
				if (cell2 != null) {

					cell2.setCellType(cell2.CELL_TYPE_STRING);
					realname = cell2.getRichStringCellValue().toString();
				}
				HSSFCell cell3 = row.getCell(3);
				if (cell3 != null) {

					cell3.setCellType(cell3.CELL_TYPE_STRING);
					qq = cell3.getRichStringCellValue().toString();
				}
				HSSFCell cell4 = row.getCell(4);
				if (cell4 != null) {

					cell4.setCellType(cell4.CELL_TYPE_STRING);
					sex = cell4.getRichStringCellValue().toString();
				}
				HSSFCell cell5 = row.getCell(5);
				if (cell5 != null) {

					cell5.setCellType(cell5.CELL_TYPE_STRING);
					qqage = cell5.getRichStringCellValue().toString();
				}
				user.setEmail(qq + "@qq.com");
				user.setUsername(qq);
				user.setRealName(realname);
				user.setQqName(qqName);
				user.setSex(sex);
				user.setQage(qqage);

				String city[] = realname.split("-");
				user.setCity(city[0]);

				user.setPassword("jsp_123456");
				userDao.save(user);
			}
			System.out.println("-------success!!!----------");
		}

		userDao.save(user);
	}




}
